<?php

namespace app\common\model;

use think\Model;

class CourseQuestion extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $table = 'think_course_question';
    
    // 设置主键
    protected $pk = 'id';
    
    // 设置字段信息
    protected $schema = [
        'id'            => 'int',
        'course_id'     => 'int',
        'question_name' => 'varchar',
        'question_type' => 'int',
        'question_data' => 'text',
        'charge_type'   => 'int',
        'admin_id'      => 'int',
        'a_time'        => 'int',
        'u_time'        => 'int'
    ];
    
    /**
     * 获取随机题目动态数据
     * @param int $limit 获取数量
     * @return array
     */
    public static function getRandomQuestionDynamics($limit = 10)
    {
        // 先获取总数
        $total = self::alias('cq')
            ->join('think_course c', 'cq.course_id = c.id')
            ->count();
            
        if ($total <= $limit) {
            // 如果总数小于等于限制数量，直接返回所有数据
            $questionDynamics = self::alias('cq')
                ->join('think_course c', 'cq.course_id = c.id')
                ->field('cq.id, cq.question_name, cq.a_time, c.name as course_name')
                ->order('cq.id desc')
                ->select();
        } else {
            // 随机偏移量
            $offset = mt_rand(0, $total - $limit);
            $questionDynamics = self::alias('cq')
                ->join('think_course c', 'cq.course_id = c.id')
                ->field('cq.id, cq.question_name, cq.a_time, c.name as course_name')
                ->order('cq.id desc')
                ->limit($offset, $limit)
                ->select();
        }
            
        return $questionDynamics ? $questionDynamics->toArray() : [];
    }
    
    /**
     * 获取最新题目动态数据
     * @param int $limit 获取数量
     * @return array
     */
    public static function getLatestQuestionDynamics($limit = 10)
    {
        $questionDynamics = self::alias('cq')
            ->join('think_course c', 'cq.course_id = c.id')
            ->field('cq.id, cq.question_name, cq.a_time, c.name as course_name')
            ->order('cq.a_time desc')
            ->limit($limit)
            ->select();
            
        return $questionDynamics ? $questionDynamics->toArray() : [];
    }
    
    /**
     * 根据ID获取题目详情
     * @param int $id 题目ID
     * @return array|null
     */
    public static function getQuestionDetail($id)
    {
        $question = self::alias('cq')
            ->join('think_course c', 'cq.course_id = c.id')
            ->join('think_exam e', 'c.exam_id = e.id')
            ->where('cq.id', $id)
            ->field('cq.*, c.name as course_name, c.code as course_code, e.name as exam_name')
            ->find();
            
        return $question ? $question->toArray() : null;
    }
    
    /**
     * 获取相关题目推荐
     * @param int $courseId 课程ID
     * @param int $excludeId 排除的题目ID
     * @param int $limit 获取数量
     * @return array
     */
    public static function getRelatedQuestions($courseId, $excludeId, $limit = 5)
    {
        $relatedQuestions = self::where('course_id', $courseId)
            ->where('id', '<>', $excludeId)
            ->field('id, question_name, question_type')
            ->order('id desc')
            ->limit($limit)
            ->select();
            
        return $relatedQuestions ? $relatedQuestions->toArray() : [];
    }
}